package com.bootdo.member.controller;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.excel.DachuExcelUtils;
import com.bootdo.common.utils.*;
import com.bootdo.mall.domain.MallOrder;
import com.bootdo.mall.service.MallOrderService;
import com.bootdo.mall_part.service.MallHousePlotService;
import com.bootdo.member.domain.ShequIntegralRecord;
import com.bootdo.member.domain.ShequMember;
import com.bootdo.member.domain.ShequMemberStatistics;
import com.bootdo.member.service.ShequIntegralRecordService;
import com.bootdo.member.service.ShequMemberService;
import com.bootdo.redis.RedisUtil;
import com.bootdo.system.domain.UserDO;
import com.front.web.commons.AvoidRepeatableCommit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * 用户表
 * 
 * @author lujingqian
 * @email 2100272728@qq.com
 * @date 2019-11-08 14:04:00
 */
 
@Controller
@RequestMapping("/member/shequMemberStatistics")
public class ShequMemberStatisticsController extends BaseController{


	@Autowired
	private ShequMemberService shequMemberService;
	@Autowired
	private ShequIntegralRecordService shequIntegralRecordService;
	@Autowired
	private MallHousePlotService mallHousePlotService;
	@Autowired
	private MallOrderService mallOrderService;
	@Autowired
	private RedisUtil redisUtil;

	@ModelAttribute
	public ShequMember get(@RequestParam(required=false) Long id) {
		if (id != null){
			return shequMemberService.get(id);
		}else{
			return new ShequMember();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/member/shequMember','member:shequMember:shequMember',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','member:shequMember:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("member:shequMember:shequMember")	
	String ShequMember(ShequMember shequMember,HttpServletRequest request,HttpServletResponse response){

		return "member/shequMember/shequMember";
	}
	@GetMapping("/add")
	@RequiresPermissions("member:shequMember:edit")
	String add(ShequMember shequMember,HttpServletRequest request,HttpServletResponse response){
	    return "member/shequMember/shequMemberedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(ShequMember shequMember) {
		return R.ok().put("i",shequMember);
	}
	@PostMapping("/list")
	@ResponseBody
	PageUtilsByLu list2(@RequestParam Map<String, Object> params,HttpServletRequest request) {
		String sso_token =  request.getHeader("sso_token");
		String qq =  request.getParameter("qq");
		if(StringUtils.isNotEmpty(qq)){
			redisUtil.set(sso_token+"statistics_all",params);
		}
		return shequIntegralRecordService.list2(params);
	}

	@ResponseBody
	@RequestMapping("/exportExcel")
	@RequiresPermissions("member:shequMember:shequMember")
	public void exportExcel(HttpServletRequest request,HttpServletResponse response){
		String sso_token =  request.getParameter("sso_token");
		if(StringUtils.isEmpty(sso_token)){
			return;
		}else{
			Map query = (Map) redisUtil.get(sso_token+"statistics_all");
			query.remove("offset");
			query.remove("limit");
			PageUtilsByLu pageUtilsByLu = shequIntegralRecordService.list2(query);
			List<ShequMemberStatistics> rows = (List<ShequMemberStatistics>)pageUtilsByLu.getRows();
			try {
				new DachuExcelUtils<ShequMemberStatistics>().exportExcle("业主消费统计-" + DateUtils.format(new Date()), ShequMemberStatistics.class, rows, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
